package com.rizzo.analytics.domain.trend;

import com.google.common.collect.Multimap;
import com.google.common.collect.Ordering;
import com.google.common.collect.TreeMultimap;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

import java.io.Serializable;
import java.util.Collection;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: jalie
 * Date: 11/23/12
 * Time: 10:05 PM
 * To change this template use File | Settings | File Templates.
 */
public class TrendedService implements Serializable {

	private static final long serialVersionUID = 9L;

	private String instanceName;

	private String instanceIndex;

	private String service;

	private Multimap<TrendType, TrendPoint> trendMultimap;

	public TrendedService() {
	}

	public TrendedService(String service, String instanceName, String instanceIndex, Multimap<TrendType, TrendPoint> trendMultimap) {
		this.service = service;
		this.instanceName = instanceName;
		this.instanceIndex = instanceIndex;
		this.trendMultimap = trendMultimap;
	}

	public String getService() {
		return service;
	}

	public void setService(String service) {
		this.service = service;
	}

	public String getInstanceName() {
		return instanceName;
	}

	public void setInstanceName(String instanceName) {
		this.instanceName = instanceName;
	}

	public String getInstanceIndex() {
		return instanceIndex;
	}

	public void setInstanceIndex(String instanceIndex) {
		this.instanceIndex = instanceIndex;
	}

	public Map<TrendType, Collection<TrendPoint>> getTrendData() {
		if (this.trendMultimap == null) {
			this.trendMultimap = TreeMultimap.create(Ordering.natural(), Ordering.natural());
		}
		return trendMultimap.asMap();
	}

	public void setTrendData(Map<TrendType, Collection<TrendPoint>> trendData) {
		for (Map.Entry<TrendType, Collection<TrendPoint>> trendCollectionEntry : trendData.entrySet()) {
			for (TrendPoint trendPoint : trendCollectionEntry.getValue()) {
				trendData().put(trendCollectionEntry.getKey(), trendPoint);
			}
		}
	}

	public Multimap<TrendType, TrendPoint> trendData() {
		if (this.trendMultimap == null) {
			this.trendMultimap = TreeMultimap.create(Ordering.natural(), Ordering.natural());
		}
		return trendMultimap;
	}

	@Override
	public boolean equals(Object other) {
		return EqualsBuilder.reflectionEquals(other, this);
	}

	@Override
	public int hashCode() {
		return HashCodeBuilder.reflectionHashCode(this);
	}

	@Override
	public String toString() {
		return new ToStringBuilder(this).
				append("instanceIndex", instanceIndex).
				append("instanceName", instanceName).
				append("service", service).
				append("trendData", trendMultimap).
				toString();
	}
}
